﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode.Models;

namespace QRCode.Controllers
{
    public class SalesPromotionQuestionController : Controller
    {
        public SalesPromotionQuestionDBContext db = new SalesPromotionQuestionDBContext();

        //
        // GET: /SalesPromotionQuestion/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index(int SalesPromotionID)
        {
            ViewBag.SalesPromotionID = SalesPromotionID;
            var questions = db.SalesPromotionQuestions.Where(b => b.SalesPromotionID == SalesPromotionID).ToList();

            //Only one SalesPromotionQuestion is allowed
            if (questions.Count == 0)
                ViewBag.AllowAdd = "Y";
            else
                ViewBag.AllowAdd = "N";

            return View(questions);
        }

        //
        // GET: /SalesPromotionQuestion/Details/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int id = 0)
        {
            SalesPromotionQuestion salespromotionquestion = db.SalesPromotionQuestions.Find(id);
            if (salespromotionquestion == null)
            {
                return HttpNotFound();
            }
            return View(salespromotionquestion);
        }

        //
        // GET: /SalesPromotionQuestion/Create

        [Authorize(Roles = "Administrator")]
        public ActionResult Create(int SalesPromotionID)
        {
            ViewBag.SalesPromotionID = SalesPromotionID;

            SalesPromotionQuestion model = new SalesPromotionQuestion();
            model.SalesPromotionID = SalesPromotionID;

            return View(model);
        }

        //
        // POST: /SalesPromotionQuestion/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(SalesPromotionQuestion salespromotionquestion)
        {
            if (ModelState.IsValid)
            {
                db.SalesPromotionQuestions.Add(salespromotionquestion);
                db.SaveChanges();
                return RedirectToAction("Index", new{ SalesPromotionID = salespromotionquestion.SalesPromotionID });
            }

            return View(salespromotionquestion);
        }

        //
        // GET: /SalesPromotionQuestion/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            SalesPromotionQuestion salespromotionquestion = db.SalesPromotionQuestions.Find(id);
            if (salespromotionquestion == null)
            {
                return HttpNotFound();
            }

            ViewBag.SalesPromotionID = salespromotionquestion.SalesPromotionID;

            return View(salespromotionquestion);
        }

        //
        // POST: /SalesPromotionQuestion/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(SalesPromotionQuestion salespromotionquestion)
        {
            if (ModelState.IsValid)
            {
                //string SalesPromotionID = Request.Form.GetValues("SalesPromotionID")[0];

               // salespromotionquestion.SalesPromotionID = Convert.ToInt32(SalesPromotionID);

                db.Entry(salespromotionquestion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { SalesPromotionID = salespromotionquestion.SalesPromotionID });
            }
            return View(salespromotionquestion);
        }

        //
        // GET: /SalesPromotionQuestion/Delete/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id = 0)
        {
            SalesPromotionQuestion salespromotionquestion = db.SalesPromotionQuestions.Find(id);
            if (salespromotionquestion == null)
            {
                return HttpNotFound();
            }
            return View(salespromotionquestion);
        }

        //
        // POST: /SalesPromotionQuestion/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Administrator")]
        public ActionResult DeleteConfirmed(int id)
        {
            SalesPromotionQuestion salespromotionquestion = db.SalesPromotionQuestions.Find(id);
            db.SalesPromotionQuestions.Remove(salespromotionquestion);
            db.SaveChanges();
            return RedirectToAction("Index", new{ SalesPromotionID = salespromotionquestion.SalesPromotionID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}